package com.javaPro.myProject.modules.article.controller;

import com.javaPro.myProject.common.controller.BaseController;
import com.javaPro.myProject.common.model.AjaxResult;
import com.javaPro.myProject.common.model.ListByPage;
import com.javaPro.myProject.common.util.uploadUtil;
import com.javaPro.myProject.modules.article.entity.TArticle;
import com.javaPro.myProject.modules.article.service.TArticleService;
import com.javaPro.myProject.modules.files.entity.TFile;
import com.javaPro.myProject.modules.files.service.TFileService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * (TArticle)表控制层
 *
 * @author makejava
 * @since  07:51:43
 */
@RestController
@RequestMapping("tArticle")
public class TArticleController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private TArticleService tArticleService;
    @Resource
    private TFileService tFileService;
    @Resource
    private uploadUtil uploadUtil;
    /**
     * 分页查询
     *
     * @param tArticle 筛选条件
     * @return 查询结果
     */
    @GetMapping("list")
    public ListByPage queryByPage(TArticle tArticle) {
        startPage();
        List<TArticle> tArticles = this.tArticleService.queryByPage(tArticle);

        return getList("查询成功",tArticles);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("detail")
    public AjaxResult queryById(Integer id,String applyer) {
        return AjaxResult.success(this.tArticleService.queryById(id,applyer));
    }

    /**
     * 新增数据
     *
     * @param tArticle 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public AjaxResult add(TArticle tArticle, MultipartFile file,MultipartFile filePic) {
        int res = 0;

        if (file != null){
            String upload = uploadUtil.upload(file);
            TFile tFile = new TFile();
            tFile.setFileName(tArticle.getFileName());
            tFile.setFilePath(upload);
            tFile.setArticleId(tArticle.getId());

            TFile insert = tFileService.insert(tFile);
            tArticle.setFileId(insert.getId());
        }
        if (filePic != null){
            String upload  = uploadUtil.upload(filePic);
            tArticle.setPic(upload);
            res  =   this.tArticleService.insert(tArticle);
        }
        return AjaxResult.success(res);
    }

    /**
     * 编辑数据
     *
     * @param tArticle 实体
     * @return 编辑结果
     */
    @PostMapping("edit")
    public AjaxResult edit(TArticle tArticle) {
        return AjaxResult.autoJudge(this.tArticleService.update(tArticle));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @PostMapping("del")
    public AjaxResult deleteById(Integer id) {
        return AjaxResult.success(this.tArticleService.deleteById(id));
    }


}

